package com.itfreer.esb.proxy;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.esb.cilent.resultinfo.IpCountInfo;
import com.itfreer.esb.cilent.resultinfo.IpCountResultInfo;
import com.itfreer.esb.cilent.resultinfo.MonthCountInfo;
import com.itfreer.esb.cilent.resultinfo.MonthCountResultInfo;
import com.itfreer.esb.cilent.resultinfo.ResultInfo;
import com.itfreer.esb.cilent.resultinfo.ServerCountInfo;
import com.itfreer.esb.cilent.resultinfo.ServerCountResultInfo;
import com.itfreer.esb.cilent.resultinfo.UserCountInfo;
import com.itfreer.esb.cilent.resultinfo.UserCountResultInfo;
import com.itfreer.esb.config.ConfigUtil;
import com.itfreer.esb.dao.IAccessCountByServerDao;
import com.itfreer.esb.dao.IAccessCountByServerIpDao;
import com.itfreer.esb.dao.IAccessCountByServerMonthDao;
import com.itfreer.esb.dao.IAccessCountByServerProxyDao;
import com.itfreer.esb.dao.IProxyAdressDao;
import com.itfreer.esb.dao.IProxyBlackDao;
import com.itfreer.esb.dao.IProxyTempAdressDao;
import com.itfreer.esb.param.EProxyType;
import com.itfreer.esb.proxy.entity.AccessCountByServerEntity;
import com.itfreer.esb.proxy.entity.AccessCountByServerIpEntity;
import com.itfreer.esb.proxy.entity.AccessCountByServerMonthEntity;
import com.itfreer.esb.proxy.entity.AccessCountByServerProxyEntity;
import com.itfreer.esb.proxy.entity.ProxyAdressEntity;
import com.itfreer.esb.proxy.entity.ProxyBlackEntity;
import com.itfreer.esb.proxy.entity.ProxyTempAdressEntity;
import com.itfreer.esb.utils.RandValueUtil;
/**
 * esb服务代理注册类
 * @author gj
 *
 */
@Component
public class EsbProxy implements IEsbProxy,IEsbGisProxy{
	
	@Autowired
	private IProxyAdressDao proxyAdressDao ;
	
	@Autowired
	private IProxyBlackDao proxyBlackDao;
	
	@Autowired
	IAccessCountByServerDao accessCountByServerDao;
	
	@Autowired
	IAccessCountByServerIpDao accessCountByServerIpDao;
	
	@Autowired
	IAccessCountByServerMonthDao accessCountByServerMonthDao;

	@Autowired
	IAccessCountByServerProxyDao accessCountByServerProxyDao;

	@Override
	public ResultInfo registerProxy(EProxyType type, String url, Date byTime) {
		ResultInfo result=new ResultInfo();
		if(url==null){
			System.out.println("原来项目地址有问题");
			result.setStatus(false);
	    	result.setContent("原来项目地址有问题");
	    	return result;
		}
	    String token =UUID.randomUUID().toString().replace("-", "");
	    String _newAdress=null;
	    if(EProxyType.Arcgis.equals(type)) {
	    	_newAdress= ConfigUtil.getArcgisServerHost()+":"+ConfigUtil.getArcgisServerPort()+"/"+token;
	    }else if(EProxyType.Function.equals(type)) {
	    	_newAdress= ConfigUtil.getFunctionServerHost()+":"+ConfigUtil.getFunctionServerPort()+"/"+token;
	    }else {
	    	result.setStatus(false);
	    	result.setContent("不支持的服务类型");
	    	return result;
	    }
	  	String oldAddress="";
	  	String address=url;
		if(address.indexOf("://")>0){
			String[] b =address.split("://");
			if(b[1].indexOf("/")>0){
				oldAddress=b[1].substring(0, b[1].indexOf("/"));
			}
		}else{
			if(address.indexOf("/")>0){
				oldAddress = address.substring(0, address.indexOf("/"));
			}
		}
		String[] oldAddresses=oldAddress.split(":");
		if(oldAddresses.length<2){
			System.out.println("原来项目地址有问题");
			result.setStatus(false);
	    	result.setContent("原来项目地址有问题");
	    	return result;
		}
		String newAddress=address.replace(oldAddress, _newAdress);  
		
		ProxyAdressEntity entity=new ProxyAdressEntity();
		entity.setId(token);
		entity.setOldAddress(url);
		entity.setOldIp(oldAddresses[0]);
		entity.setOldPort(oldAddresses[1]);
		entity.setProxyUse("1");
		entity.setServerUse("1");
		entity.setNewAddress(newAddress);
		entity.setNewIp(_newAdress);
		entity.setOverTime(byTime);
		entity.setServerType(type.toString());
		proxyAdressDao.add(entity);
		result.setContent(newAddress);
		result.setStatus(true);
		return result;
	}

	@Override
	public ResultInfo modifyProxy(EProxyType type, String proxyUrl, boolean canuse, Date byTime) {
		HashMap<String,Object> where=new HashMap<String,Object>();
		where.put("newAddress:=", proxyUrl);
		ResultInfo result=new ResultInfo();
		
		List<ProxyAdressEntity> entitys=proxyAdressDao.getEntitys(null, where, null, 1000000, 1);
		if(entitys==null || entitys.size()<1) {
			result.setStatus(false);
	    	result.setContent("代理服务没有找到");
	    	return result;
		}
		
		ProxyAdressEntity entity=entitys.get(0);
		if(canuse) {
			entity.setProxyUse("1");
		}else {
			entity.setProxyUse("0");
		}
		entity.setOverTime(byTime);
		proxyAdressDao.update(entity);
		result.setStatus(true);
		return result;
	}

	@Override
	public ResultInfo modify(EProxyType type, String Url, boolean canuse) {
		HashMap<String,Object> where=new HashMap<String,Object>();
		where.put("oldAddress:=", Url);
		ResultInfo result=new ResultInfo();
		
		List<ProxyAdressEntity> entitys=proxyAdressDao.getEntitys(null, where, null, 1000000, 1);
		if(entitys==null || entitys.size()<1) {
			result.setStatus(false);
	    	result.setContent("服务没有找到");
	    	return result;
		}
		for(ProxyAdressEntity entity:entitys) {
			if(canuse) {
				entity.setServerUse("1");
			}else {
				entity.setServerUse("0");
			}
			proxyAdressDao.update(entity);
		}
		result.setStatus(true);
		return result;
	}
	
	/**
	 * 服务访问统计（合计）
	 */
	@Override
	public ResultInfo getUseCount(EProxyType type, String Url) {
		HashMap<String,Object> where=new HashMap<String,Object>();
		where.put("server:=", Url);
		ResultInfo result=new ResultInfo();
		
		List<AccessCountByServerEntity> lst=accessCountByServerDao.getEntitys(null, where, null, 1, 1);
		if(lst==null || lst.size()<1) {
			result.setContent("0");
		}else {
			result.setContent(lst.get(0).getCount()+"");
		}
		result.setStatus(true);
		return result;
	}

	@Override
	public IpCountResultInfo getUseCountByIp(EProxyType type, String Url,int n) {
	
		HashMap<String,Object> where=new HashMap<String,Object>();
		where.put("server:=", Url);
		
		HashMap<String,Integer> order=new HashMap<String,Integer>();
		order.put("count", 0);
		
		IpCountResultInfo result=new IpCountResultInfo();
		
		if(n<1) {
			result.setStatus(false);
			result.setContent("个数必须大于0");
			return result;
		}
		List<AccessCountByServerIpEntity> lst=accessCountByServerIpDao.getEntitys(null, where, order, n, 1);
		if(lst==null || lst.size()<1) {
			
		}else {
			for(AccessCountByServerIpEntity ientity:lst) {
				IpCountInfo info=new IpCountInfo();
				info.setIp(ientity.getIp());
				info.setCount(ientity.getCount());
				result.getLst().add(info);
			}
		}
		result.setStatus(true);
		return result;
	}

	@Override
	public ResultInfo delete(EProxyType type, String Url) {
		HashMap<String,Object> where=new HashMap<String,Object>();
		where.put("oldAddress:=", Url);
		ResultInfo result=new ResultInfo();
		
		List<ProxyAdressEntity> entitys=proxyAdressDao.getEntitys(null, where, null, 1000000, 1);
		if(entitys==null || entitys.size()<1) {
			result.setStatus(false);
	    	result.setContent("服务没有找到");
	    	return result;
		}
		for(ProxyAdressEntity entity:entitys) {
			proxyAdressDao.delete(entity.getId());
		}
		result.setStatus(true);
		return result;
	}

	@Override
	public ResultInfo deleteProxy(EProxyType type, String proxyUrl) {
		HashMap<String,Object> where=new HashMap<String,Object>();
		where.put("newAddress:=", proxyUrl);
		ResultInfo result=new ResultInfo();
		
		List<ProxyAdressEntity> entitys=proxyAdressDao.getEntitys(null, where, null, 1000000, 1);
		if(entitys==null || entitys.size()<1) {
			result.setStatus(false);
	    	result.setContent("代理服务没有找到");
	    	return result;
		}
		for(ProxyAdressEntity entity:entitys) {
			proxyAdressDao.delete(entity.getId());
		}
		result.setStatus(true);
		return result;
	}

	@Override
	public ResultInfo AddBlackList(EProxyType type, String Url, List<String> blackList) {
		ResultInfo result=new ResultInfo();
		if(Url==null) {
			result.setStatus(false);
			result.setContent("服务地址有误");
		}
		
		if(blackList==null || blackList.size()<1) {
			result.setStatus(false);
			result.setContent("黑名单ip地址为空");
		}
		for(String iblack:blackList) {
			ProxyBlackEntity entity=new ProxyBlackEntity();
			entity.setId(RandValueUtil.getGuid());
			entity.setServer(Url);
			entity.setBlack(iblack);
			entity.setTime(new Date());
			entity.setUse("1");
			proxyBlackDao.add(entity);
		}
		result.setStatus(true);
		return result;
	}

	@Override
	public ResultInfo removeBlackList(EProxyType type, String Url, List<String> blackList) {
		ResultInfo result=new ResultInfo();
		if(Url==null) {
			result.setStatus(false);
			result.setContent("服务地址有误");
		}
		
		if(blackList==null || blackList.size()<1) {
			result.setStatus(false);
			result.setContent("黑名单ip地址为空");
		}
		HashMap<String,Object> where=new HashMap<String,Object>();
		
		String queryStr="";
		for(String iblack:blackList) {
			if("".equals(queryStr))queryStr=""+iblack+"";
			else {
				queryStr=queryStr+","+iblack+"";
			}
		}
		where.put("server:=", Url);
		where.put("black:in", queryStr);
		List<ProxyBlackEntity> lst=proxyBlackDao.getEntitys(null, where,null,1000000,1);
		if(lst!=null && lst.size()>0) {
			for(ProxyBlackEntity ientity:lst) {
				proxyBlackDao.delete(ientity.getId());
			}
		}
		result.setStatus(true);
		return result;
	}

	@Override
	public MonthCountResultInfo getUseCountByMonth(EProxyType type, String Url, int n) {

		HashMap<String,Object> where=new HashMap<String,Object>();
		where.put("server:=", Url);
		
		HashMap<String,Integer> order=new HashMap<String,Integer>();
		order.put("month", 0);
		
		MonthCountResultInfo result=new MonthCountResultInfo();
		
		if(n<1) {
			result.setStatus(false);
			result.setContent("个数必须大于0");
			return result;
		}
		List<AccessCountByServerMonthEntity> lst=accessCountByServerMonthDao.getEntitys(null, where, order, n, 1);
		if(lst==null || lst.size()<1) {
			
		}else {
			for(AccessCountByServerMonthEntity ientity:lst) {
				MonthCountInfo info=new MonthCountInfo();
				info.setCount(ientity.getCount());
				info.setMonth(ientity.getMonth());
				result.getLst().add(info);
			}
		}
		result.setStatus(true);
		return result;
	}

	@Override
	public UserCountResultInfo getUseCountByProxy(EProxyType type, String Url, int n) {
		HashMap<String,Object> where=new HashMap<String,Object>();
		where.put("server:=", Url);
		
		HashMap<String,Integer> order=new HashMap<String,Integer>();
		order.put("count", 0);
		
		UserCountResultInfo result=new UserCountResultInfo();
		
		if(n<1) {
			result.setStatus(false);
			result.setContent("个数必须大于0");
			return result;
		}
		List<AccessCountByServerProxyEntity> lst=accessCountByServerProxyDao.getEntitys(null, where, order, n, 1);
		if(lst==null || lst.size()<1) {
			
		}else {
			for(AccessCountByServerProxyEntity ientity:lst) {
				UserCountInfo info=new UserCountInfo();
				info.setCount(ientity.getCount());
				info.setProxy(ientity.getProxy());
				result.getLst().add(info);
			}
		}
		result.setStatus(true);
		return result;
	}

	@Override
	public ServerCountResultInfo getUseCountByServer(int n) {
		HashMap<String,Integer> order=new HashMap<String,Integer>();
		order.put("count", 0);
		
		ServerCountResultInfo result=new ServerCountResultInfo();
		
		if(n<1) {
			result.setStatus(false);
			result.setContent("个数必须大于0");
			return result;
		}
		List<AccessCountByServerEntity> lst=accessCountByServerDao.getEntitys(null, null, order, n, 1);
		if(lst==null || lst.size()<1) {
			
		}else {
			for(AccessCountByServerEntity ientity:lst) {
				ServerCountInfo info=new ServerCountInfo();
				info.setCount(ientity.getCount());
				info.setServer(ientity.getServer());
				result.getLst().add(info);
			}
		}
		result.setStatus(true);
		return result;
	}
	/**
	 * 临时服务保存问题
	 */
	@Autowired
	private IProxyTempAdressDao proxyTempAdressDao ;
	
	/**
	 * 注册临时服务
	 */
	@Override
	public ResultInfo registerTemp(EProxyType type, String url, String antiTheftChain, Date byTime) {
		ResultInfo result=new ResultInfo();
		if(url==null){
			System.out.println("原来项目地址有问题");
			result.setStatus(false);
	    	result.setContent("原来项目地址有问题");
	    	return result;
		}
	    String token =UUID.randomUUID().toString().replace("-", "");
	    String _newAdress=null;
	    if(EProxyType.Arcgis.equals(type)) {
	    	_newAdress= ConfigUtil.getArcgisServerTempHost()+":"+ConfigUtil.getArcgisServerTempPort()+"/"+token;
	    }else if(EProxyType.Function.equals(type)) {
	    	_newAdress= ConfigUtil.getFunctionServerHost()+":"+ConfigUtil.getFunctionServerPort()+"/"+token;
	    }else {
	    	result.setStatus(false);
	    	result.setContent("不支持的服务类型");
	    	return result;
	    }
	  	String oldAddress="";
	  	String address=url;
		if(address.indexOf("://")>0){
			String[] b =address.split("://");
			if(b[1].indexOf("/")>0){
				oldAddress=b[1].substring(0, b[1].indexOf("/"));
			}
		}else{
			if(address.indexOf("/")>0){
				oldAddress = address.substring(0, address.indexOf("/"));
			}
		}
		String[] oldAddresses=oldAddress.split(":");
		if(oldAddresses.length<2){
			System.out.println("原来项目地址有问题");
			result.setStatus(false);
	    	result.setContent("原来项目地址有问题");
	    	return result;
		}
		String newAddress=address.replace(oldAddress, _newAdress);  
		
		ProxyTempAdressEntity entity=new ProxyTempAdressEntity();
		entity.setId(token);
		entity.setOldAddress(url);
		entity.setOldIp(oldAddresses[0]);
		entity.setOldPort(oldAddresses[1]);
		entity.setNewAddress(newAddress);
		entity.setNewIp(_newAdress);
		entity.setOverTime(byTime);
		entity.setServerType(type.toString());
		entity.setReferer(antiTheftChain);
		proxyTempAdressDao.add(entity);
		result.setContent(newAddress);
		result.setStatus(true);
		return result;
	}

}
